/*
 * ftp_put.c
 * Michael du Breuil
 */

#include "common.h"
#include "ftp_put.h"

CURL *ftp_curl;
ftp_settings_t ftp_settings;
pthread_mutex_t ftp_mutex;
pthread_cond_t  ftp_cond;


int ftp_init() {
  pthread_mutex_init(&ftp_mutex, NULL);
  pthread_mutex_lock(&ftp_mutex);
  pthread_cond_init(&ftp_cond, NULL);

  ftp_curl = curl_easy_init();
  if(!ftp_curl) {
    fprintf(stderr, "ERROR: ftp_init() curl failed to start\n");
    return FAILURE;
  }

  strncpy(ftp_settings.destination, "ftp://192.168.1.102/", 128);
  ftp_settings.flags = FLAG_PAUSE;

  pthread_mutex_unlock(&ftp_mutex);
  return SUCCESS;
}

void ftp_shutdown() {
  pthread_mutex_lock(&ftp_mutex);

  ftp_settings.flags = FLAG_ABORT;
  curl_easy_cleanup(ftp_curl);
  pthread_cond_destroy(&ftp_cond);

  pthread_mutex_unlock(&ftp_mutex);
  pthread_mutex_destroy(&ftp_mutex);
}

static int ftpPut(FILE *data, char *name) {
  CURLcode success;
  curl_easy_setopt(ftp_curl, CURLOPT_UPLOAD, 1);
  curl_easy_setopt(ftp_curl, CURLOPT_URL, name);
  curl_easy_setopt(ftp_curl, CURLOPT_USERPWD, "test:awesome");
  curl_easy_setopt(ftp_curl, CURLOPT_READFUNCTION, NULL);
  curl_easy_setopt(ftp_curl, CURLOPT_READDATA, data);

  success = curl_easy_perform(ftp_curl);

  if(success != CURLE_OK)
    return FAILURE;
  return SUCCESS;
}


void *ftp_run() {
  FILE *data;
  char dest[MAX_PATH];
  char dest2[MAX_PATH];
  int success;

  while(!(ftp_settings.flags & FLAG_ABORT)) {
    pthread_mutex_lock(&ftp_mutex);

    while(ftp_settings.flags & FLAG_PAUSE) {
      pthread_cond_wait(&ftp_cond, &ftp_mutex);
    }

    snprintf(dest, sizeof(dest), "%s", ftp_settings.destination);

    pthread_mutex_unlock(&ftp_mutex); //let it go, settings might change before there is image content, don't deadlock at that point

    pthread_mutex_lock(&image_mutex);

    if(image_head == image_tail) {
      pthread_cond_wait(&image_cond, &image_mutex);
      if(image_head == image_tail) {
        //if we waited, got woken up but didn't have an image start over
        //this mostly is to get a chance at checking settings again
        pthread_mutex_unlock(&image_mutex);
        continue;
      }
    }

    data = fopen(images[image_head].fileName, "r");
    if(!data) {
      //this could be adapted to instead cycle to the next valid one, instead of starting over, but for now this is fine, and we really do want to know if this happens
      fprintf(stderr, "ERROR: ftp_run() an image we reported as saved can not be read, advancing buffer\n");
      if(image_head >= MAX_QUEUED_IMAGES - 1) 
        image_head = 0;
      else
        image_head++;
      pthread_mutex_unlock(&image_mutex);
      continue;
    }

    snprintf(dest2, sizeof(dest2), "%s%s", dest, images[image_head].fileName);

    //FIXME: grab all other needed data now, but atm none of it is used...
    if(image_head >= MAX_QUEUED_IMAGES - 1)
        image_head = 0;
    else
      image_head++;

    pthread_mutex_unlock(&image_mutex);

    success = ftpPut(data, dest2);
    if(success != SUCCESS)
      fprintf(stderr, "ERROR: ftp_run() failed to upload a file\n");


    fclose(data);

  }

  pthread_exit(NULL);
}
